<?php

namespace addons\qingdongams\model;

use think\Model;
use traits\model\SoftDelete;


/**
 *订单
 */
class Order extends Model
{
    use SoftDelete;

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    protected $name = 'qingdongams_order';
    protected $append = [
        'show_staff_data',
    ];
    public function getShowStaffDataAttr($value,$data)
    {
        if (!isset($data['show_staff_id'])) {
            return '';
        }
        $ids=explode(',',$data['show_staff_id']);
        return Staff::where(['id'=>['in',$ids]])->field('id,name,img,post,mobile')->select();
    }
    //获取更新时间
    public function getCreatetimeAttr($value) {
        return date('Y-m-d H:i:s', $value);
    }

    //负责人
    public function createStaff() {
        return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,name');
    }

    //负责人
    public function ownerStaff() {
        return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name');
    }


    //客户
    public function customer() {
        return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name,follow,address');
    }


    //联系人
    public function contacts() {
        return $this->hasOne(Contacts::class, 'id', 'contacts_id')->field('id,name');
    }

    //订单
    public function contract() {
        return $this->hasOne(Contract::class, 'id', 'contract_id')
            ->field('id,name,num,money');
    }

    //产品
    public function product(){
        return $this->hasMany(ContractProduct::class,'contract_id','contract_id')->with('product');
    }


}
